package com.tengju.support.infrastructure.repository.rule.convert;

import com.fasterxml.jackson.core.type.TypeReference;
import com.tengju.support.domain.model.rule.BizType;
import com.tengju.support.domain.model.rule.Rule;
import com.tengju.support.domain.model.rule.RuleId;
import com.tengju.support.domain.model.rule.RuleNode;
import com.tengju.support.domain.shared.JsonUtil;
import com.tengju.support.infrastructure.persistence.mybatis.dataobject.SupportRuleDO;

import java.time.LocalDateTime;
import java.util.List;

public class RuleConvert {

    public static SupportRuleDO convertToDO(Rule rule) {

        if (rule == null) {
            return null;
        }

        SupportRuleDO supportRuleDO = new SupportRuleDO();
        supportRuleDO.setBizType(rule.getBizType().getVal());
        supportRuleDO.setDescription(rule.getDescription());
        supportRuleDO.setRuleInfo(JsonUtil.toJSONString(rule.getRuleNodes()));
        supportRuleDO.setCreateTime(LocalDateTime.now());
        supportRuleDO.setUpdateTime(LocalDateTime.now());
        supportRuleDO.setCreateBy(rule.getCreateBy());
        supportRuleDO.setUpdateBy(rule.getUpdateBy());

        return supportRuleDO;
    }


    public static Rule convertToModel(SupportRuleDO ruleDO) {


        List<RuleNode> ruleNodes = JsonUtil.toList(ruleDO.getRuleInfo(), new TypeReference<>() {
        });

        return new Rule(new RuleId(ruleDO.getId()),
                ruleDO.getDescription(),
                ruleNodes,
                BizType.fromVal(ruleDO.getBizType()),
                ruleDO.getCreateBy(),
                ruleDO.getUpdateBy());

    }


    public static void main(String[] args) {
        String s = "[{\"condition\":{\"@Clazz\":\"com.tengju.support.domain.model.rule.Condition\",\"operator\":\"and\",\"child\":[{\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\",\"factors\":[\"30\"],\"operation\":\"IN\",\"field\":\"targetStarLevel\",\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\"},{\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\",\"factors\":[\"2\"],\"operation\":\">=\",\"field\":\"developCitySevicerNum\",\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\"}],\"@Clazz\":\"com.tengju.support.domain.model.rule.Condition\"},\"actions\":[]},{\"condition\":{\"@Clazz\":\"com.tengju.support.domain.model.rule.Condition\",\"operator\":\"and\",\"child\":[{\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\",\"factors\":[\"40\"],\"operation\":\"IN\",\"field\":\"targetStarLevel\",\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\"},{\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\",\"factors\":[\"4\"],\"operation\":\">=\",\"field\":\"developCitySevicerNum\",\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\"}],\"@Clazz\":\"com.tengju.support.domain.model.rule.Condition\"},\"actions\":[]},{\"condition\":{\"@Clazz\":\"com.tengju.support.domain.model.rule.Condition\",\"operator\":\"and\",\"child\":[{\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\",\"factors\":[\"60\"],\"operation\":\"IN\",\"field\":\"targetStarLevel\",\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\"},{\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\",\"factors\":[\"6\"],\"operation\":\">=\",\"field\":\"developCitySevicerNum\",\"@Clazz\":\"com.tengju.support.domain.model.rule.MathCondition\"}],\"@Clazz\":\"com.tengju.support.domain.model.rule.Condition\"},\"actions\":[]}]";

        List<RuleNode> ruleNodes = JsonUtil.toList(s, new TypeReference<>() {
        });

        System.out.println(ruleNodes);
    }
}
